﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL.DBHelpers;

namespace QuanLyKhachHang
{
    public partial class uc_KhachHang : UserControl
    {
        public uc_KhachHang()
        {
            InitializeComponent();
        }
        string ID = string.Empty;
        string MaKh = string.Empty;
        public void loaddata()
        {
            var dt = DBHelper.GetDataTableSP("sp_KhanhHang_Get");
            gdKh.DataSource = dt;
        }
        public void resetfield()
        {
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtMaKH.Text = "";
            txtMota.Text = "";
            txtSDT.Text = "";
            txtTenCty.Text = "";
            txtTenKH.Text = "";
            txtTuoi.Text = "";
            txtMaKH.Enabled = true;
            
        }
        private void uc_KhachHang_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            string mkh = string.Empty;
            if (DBHelper.CheckExitInDB(txtMaKH.Text,"MaKH","sp_KhanhHang_Get")==true) 
            {
                MessageBox.Show("Mã khách hàng đã tồn tại !");
                return;
            }
            else mkh = txtMaKH.Text;
            ls.Add(new SqlParameter("@MaKH",mkh));
            ls.Add(new SqlParameter("@TenKH",txtTenKH.Text));
            ls.Add(new SqlParameter("@TenCty",txtTenCty.Text));
            ls.Add(new SqlParameter("@Tuoi",txtTuoi.Text));
            ls.Add(new SqlParameter("@Email",txtEmail.Text));
            ls.Add(new SqlParameter("@SDT",txtSDT.Text));
            ls.Add(new SqlParameter("@DiaChi",txtDiaChi.Text));
            ls.Add(new SqlParameter("@MoTa",txtMota.Text));
            DBHelper.ExecuteNonQuery("sp_KhachHang_Add", ls);
            loaddata();
            resetfield();
            MessageBox.Show("Thêm thành công !");

        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            string mkh = string.Empty;
            
            mkh = txtMaKH.Text;
            ls.Add(new SqlParameter("@MaKH", mkh));
            ls.Add(new SqlParameter("@TenKH", txtTenKH.Text));
            ls.Add(new SqlParameter("@TenCty", txtTenCty.Text));
            ls.Add(new SqlParameter("@Tuoi", txtTuoi.Text));
            ls.Add(new SqlParameter("@Email", txtEmail.Text));
            ls.Add(new SqlParameter("@SDT", txtSDT.Text));
            ls.Add(new SqlParameter("@DiaChi", txtDiaChi.Text));
            ls.Add(new SqlParameter("@MoTa", txtMota.Text));
            DBHelper.ExecuteNonQuery("sp_KhachHang_Update", ls);
            loaddata();
            resetfield();
            MessageBox.Show("Cập nhật thành công !");
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", ID));
            DBHelper.ExecuteNonQuery("sp_KhachHang_Delete",ls);
            loaddata();
            resetfield();
            MessageBox.Show("Xóa Thành công !");
        }

        private void BtnExport_Click(object sender, EventArgs e)
        {
            gdKh.ShowPrintPreview();
        }

        private void gdKh_Click(object sender, EventArgs e)
        {
            txtMaKH.Enabled = false;
            MaKh = gdvNV.GetFocusedRowCellDisplayText(col_MaHK); 
            ID = gdvNV.GetFocusedRowCellDisplayText(col_Id);
            txtDiaChi.Text = gdvNV.GetFocusedRowCellDisplayText(col_DiaChi);
            txtEmail.Text = gdvNV.GetFocusedRowCellDisplayText(col_Email);
            txtMaKH.Text = gdvNV.GetFocusedRowCellDisplayText(col_MaHK);
            txtMota.Text = gdvNV.GetFocusedRowCellDisplayText(col_MoTa);
            txtSDT.Text = gdvNV.GetFocusedRowCellDisplayText(col_SDT);
            txtTenCty.Text = gdvNV.GetFocusedRowCellDisplayText(col_TenCT);
            txtTenKH.Text = gdvNV.GetFocusedRowCellDisplayText(col_TenKH);
            txtTuoi.Text = gdvNV.GetFocusedRowCellDisplayText(col_Tuoi);
            
        }
    }
}
